import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBBaseRouter/YBApplication.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBBaseRouter/YBNavigator.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBColor/YBColor.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBImageStyle/YBImageStyle.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBTextStyle/YBTextStyle.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/yb_chinese_text/yb_chinese_text.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBJsonToModel/YBBaseModel.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBFindModule/YBFindProvider/YBLocationInfoEventBus.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBHomePage/YBHomePageProvider/YBCitySelectEventBus.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBHomePage/YBSubPage/YBCitySelectPage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBAPi/YBAPi.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBFindFaXianNetWork/YBFindFaXianNetWork.dart';
class YBPublishQuanCengInfoPage extends StatefulWidget {

  final String urlStr;

  const YBPublishQuanCengInfoPage({Key key, this.urlStr}) : super(key: key);

  @override
  _YBPublishQuanCengInfoPageState createState() => _YBPublishQuanCengInfoPageState(urlStr: urlStr);
}

class _YBPublishQuanCengInfoPageState extends State<YBPublishQuanCengInfoPage> {

  final String urlStr;

  TextEditingController _editingController = TextEditingController();

  _YBPublishQuanCengInfoPageState({this.urlStr});

  String _locationStr = YBChineseText.homeTextAddress;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    YBCitySelectPage().requestLocation();
    eventBus.on<YBCitySelectEventBus>().listen((event) {
      print('--------${event.cityName}-');
      setState(() {
        _locationStr = event.cityName;
      });
    });

    locationInfoEventBus.on<YBLocationInfoEventBus>().listen((event) {
      setState(() {
        _locationStr = event.cityTitle;
      });
    });
  }


  @override
  Widget build(BuildContext context) {
    Widget listWidget;
    if(urlStr.contains("圈层") == true){
      listWidget = SliverToBoxAdapter();
    }else{
       List<String> urlStrinList = urlStr.split(",");
      listWidget = SliverPadding(
          padding:EdgeInsets.only(left: YBTextStyle.adapterSize20,right: YBTextStyle.adapterSize20),
          sliver: SliverGrid(
              delegate:SliverChildBuilderDelegate((BuildContext context,int index){
                return Container(
                  child: CachedNetworkImage(imageUrl:"${YBApi.Base_image_URL}/${urlStrinList[index]}",fit: BoxFit.fitWidth, height: 140,),
                );
              },childCount: urlStrinList.length), gridDelegate:SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 3,
            mainAxisSpacing: YBTextStyle.adapterSize10,
            childAspectRatio: 1.5,
            crossAxisSpacing: YBTextStyle.adapterSize10,
          )),
      );
    }


    return Container(
      child: Scaffold(
        appBar: AppBar(
          elevation: 0,
          leading: Container(
            child:InkWell(
              child: Padding(
                padding: EdgeInsets.only(left: YBTextStyle.adapterSize10),
                child: Center(
                  child: Text('取消',style: YBTextStyle.TextStyle7A7A7A12,),
                ),
              ),
              onTap: () async {
                YBNavigatorUtil.goDefaultBack(context);
              },
            ),
          ),
          actions: [
            Container(
              child:InkWell(
                child: Padding(
                  padding: EdgeInsets.only(right: YBTextStyle.adapterSize10),
                  child: Center(
                    child: Text('完成',style:YBTextStyle.TextStyle7A7A7A12,),
                  ),
                ),
                onTap: () async {

                  if (urlStr != null){
                    _setPublishInfo(title: _editingController.text,imageUrl: urlStr,area:_locationStr);
                  }else{
                    _setPublishInfo(title:_editingController.text, area:_locationStr);
                  }
                },
              ),
            ),
          ],
        ),
        body: CustomScrollView(
          slivers: [
            SliverToBoxAdapter(
              child: Container(
                height: YBTextStyle.adapterSize10,
              ),
            ),
            SliverToBoxAdapter(
              child: Container(
                color: YBColor.white,
                height: YBTextStyle.adapterSize300,
                child:Padding(
                  padding: EdgeInsets.only(left: YBTextStyle.adapterSize20,right: YBTextStyle.adapterSize20),
                  child: TextField(
                    decoration: InputDecoration(
                      border: InputBorder.none,
                      hintText: YBChineseText.findQingShuoNinXiangShuoDeHuaTitle,
                    ),
                    maxLines: 10,
                    controller: _editingController,
                  ),
                ),
              ),
            ),
            listWidget,
            SliverToBoxAdapter(
              child: Container(
                height: YBTextStyle.adapterSize10,
              ),
            ),
            SliverToBoxAdapter(
              child: InkWell(
                child: Container(
                  color: YBColor.white,
                  height: YBTextStyle.adapterSize44,
                  child: Padding(
                    padding: EdgeInsets.only(left: YBTextStyle.adapterSize20,right: YBTextStyle.adapterSize20),
                    child: Row(
                      children: [
                        Container(
                          child: Image.asset(YBImageStyle.getImagePath('')),
                        ),
                        Container(
                          child: Text(_locationStr??"全国"),
                        ),
                        Spacer(),
                        Container(
                          child: Image.asset(YBImageStyle.getImagePath('findIconArrowRight')),
                        ),
                      ],
                    ),
                  ),
                ),
                onTap: () async {
                  YBApplication.router.navigateTo(context, '/findQuanCengPage/findQuanCengDetailWebViewPage/findQuanCengAddressSelectPage');
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  void _setPublishInfo({String title,String imageUrl,String area}){



    Map<String,dynamic> publishMap = Map<String,dynamic>();
    publishMap['sysCode'] = "xg_youbang";
    if(title != null) {
      publishMap['title'] = title;
    }

    if (imageUrl != null){
      publishMap['imageUrl'] = imageUrl;
    }

    if (area != null){
      publishMap['area'] = area;
    }


    YBFindFaXianNetWork.findFaXianPublishData(publishMap,success: (data){
      print("- 发布者信息和内容-$data---");
      YBBaseModel baseModel = YBBaseModel.fromJson(data);
      if (baseModel.code == 0){
         EasyLoading.showSuccess("发布成功");
         YBNavigatorUtil.goDefaultBack(context);
      }
    },fail: (error){
      print("- 发布者信息和内容-$error---");
    });
  }

}

